<%@page import="com.project.util.StringUtil"%>
<%@ page language="java" contentType="text/html; charset=utf-8"
	pageEncoding="utf-8"%>
<%
	String result = (String) request.getAttribute(StringUtil.RESULT);
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<%@ include file="/WEB-INF/jsp/include/head.jsp"%>
<title>找回密码</title>
</head>
<body>
<div class="container" >
	<%@ include file="/WEB-INF/jsp/include/top.jsp"%>
	<input type="hidden" value="<%=basePath %>" id="basepath">
	<input type="hidden" value="" id="returnedCode">
	<div class="margin_top col-lg-6 col-md-6 col-md-push-3 col-xs-12 col-sm-12">
		<table class="table" id="sendCode">
			<tr>
				<td>验证邮箱：</td>
				<td><input type="text" name="email" id="email"
					placeholder="验证邮箱"></td>
			</tr>
			<tr>
				<td>验证码：</td>
				<td><input type="text" name="code" id="code"
					placeholder="验证码"><label  id="codeSubmit" class="btn">发送</label></td>
			</tr>
		</table>
		<form action="ajax/findPassword" method="post" id="findForm">
<!-- 			<table class="table" id=""> -->
<!-- 				<tr> -->
<!-- 					<td>验证邮箱：</td> -->
<!-- 					<td><input type="text" name="email" id="email" -->
<!-- 						placeholder="验证邮箱"></td> -->
<!-- 				</tr> -->
<!-- 				<tr> -->
<!-- 					<td>验证码：</td> -->
<!-- 					<td><input type="text" name="code" id="code" -->
<!-- 						placeholder="验证码"><label  id="codeSubmit" class="btn">发送</label></td> -->
<!-- 				</tr> -->
<!-- 			</table> -->
			<table class="table" id="setPwd">
				<tr>
					<td>用户名：</td>
					<td><input name="id" id="id" placeholder="用户名"></td>
				</tr>
				<tr>
					<td>密码：</td>
					<td><input type="password" name="password" id="password"
						placeholder="新密码"></td>
				</tr>
				<tr>
					<td>确认密码：</td>
					<td><input type="password" name="" id="password2"
						placeholder="确认密码"></td>
				</tr>
				<tr>
					<td><button type="reset" id="" class="btn reset">重设</button> </td>
					<td><label  id="submit" class="btn submit">确认修改</label> </td>
				</tr>
			</table>
		</form>
	</div>
	<div>
		<%
			if (result == StringUtil.FAILURE) {
		%>
		<b>密码修改失败，请重试！</b>
		<%
			}
		%>
	</div>
	<%@ include file="/WEB-INF/jsp/include/bottom.jsp"%>
	</div>
</body>
<script type="text/javascript">
	$(function() {
		var basepath = $("#basepath").val().trim();
// 		alert(basepath);
		var $setPwd = $("#setPwd");
		$setPwd.hide();
		//send validation code
		var $codeSubmit = $("#codeSubmit");
		$codeSubmit.click(function(){
			var email = $("#email").val().trim();
			if(email == null || email == ""){
				alert("验证邮箱不能为空!"); 
				return;
			}
			//判断是否为邮箱号
			var reg_email = /^([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+@([a-zA-Z0-9]+[_|\_|\.]?)*[a-zA-Z0-9]+\.[a-zA-Z]{2,3}$/;
			if(!(reg_email.test(email))){
				alert("请输入正确的邮箱号!");
				return;
			}
			//按钮不能点击
			$codeSubmit.attr("disabled", true);
			var url = basepath + "/ajax/sendCode?email="+ email;
			$.ajax({
				url : url,
				type : "POST",
				data : {},
				contentType : "application/json",
				dataType : "json",
				cache : false,
				success : function(data) {
					var d = eval("(" + data + ")");//将数据转换成json类型
					alert("发送成功!");
					$("#returnedCode").val(d.code);
						//显示修改form
// 						$setPwd.show();
				},
				error : function(data) {
					alert(data.responseText);
				}
			});
		});
		
		var $code = $("#code");
		$code.blur(function() {//失去焦点
			var code  = $("#code").val().trim();
			if(code == null || code == ""){
				alert("用户id不能为空!"); 
				return;
			}
			var returnedCode = $("#returnedCode").val();
			var url = basepath + "/ajax/getMD5?code=" + code;
			$.ajax({
				url : url,
				type : "POST",
				data : {},
				contentType : "application/json",
				dataType : "json",
				cache : false,
				success : function(data) {
					var d = eval("(" + data + ")");//将数据转换成json类型
					var code = d.code;
					if(code != returnedCode){
						alert("验证码错误!");
					}else{
						alert("验证码正确!");
						//显示修改form
						$setPwd.show();
					}
				},
				error : function(data) {
					alert(data.responseText);
				}
			});		
		});

		//form 提交
		var $submit = $("#submit");
		$submit.click(function(){
			var $form = $("#findForm");
			var id = $("#id").val().trim();
			var psw1 = $("#password").val().trim();
			var psw2 = $("#password2").val().trim();
			//判断用户名、密码是否为空
			if(id == null || id == ""){
				alert("用户id不能为空!"); 
				return;
			}
			if(psw1 == null || psw1 == ""){
				alert("请输入密码!"); 
				return;
			}
			//判断密码是否一致
			if(psw1 != psw2){
				alert("两次密码输入不一致");
				return;
			}
			var url = basepath + "/ajax/findPassword?id=" + id +"&password="+ psw1;
			$.ajax({
				url : url,
				type : "POST",
				data : {},
				contentType : "application/json",
				dataType : "json",
				cache : false,
				success : function(data) {
					var d = eval("(" + data + ")");//将数据转换成json类型
					var result = d.result;
					if(result != "success"){
						alert("密码修改失败");
					}else{
						alert("修改成功!");
						//显示修改form
						window.location.href=basepath + "/user/indexJSP"; 
					}
				},
				error : function(data) {
					alert(data.responseText);
				}
			});
		});
		
	});
</script>
</html>